<template>
  <div class='container'>
<!--    遮罩层-->
    <div class="mask" v-if="isDiv">
      <el-button circle class="location" @click="close">X</el-button>
      <video :src="url" autoplay="autoplay" class="video" style="width: 50%"></video>
    </div>
<!--    卡片区域-->
    <el-card class="box-card">
      <div class="form-left">
        <el-form label-width="80px" class="form-div">
          <el-form-item label="关键字">
            <el-input placeholder="根据文章标题搜索" v-model="container.keyword"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select placeholder="请选择" v-model="container.state">
              <el-option label="已启用" value="1"></el-option>
              <el-option label="已禁用" value="0"></el-option>
            </el-select>
          </el-form-item>
          <!-- 搜索按钮 -->
          <el-button size="small" class="form-button" @click="qingBtn">清除</el-button>
          <el-button type="primary" size="small" @click='seekBtn'>搜索</el-button>
        </el-form>
        <el-button type="success" size="small" icon="el-icon-edit" @click="newlyBtn">新增技巧</el-button>
      </div>
      <!-- 数据提示 -->
      <div class="slot-icon">
        <i class="el-icon-info"></i>
        <span class="tab-span">数据一共 {{ total }} 条</span>
      </div>
      <el-table
        :data="containerQj"
        style="width: 100%">
        <el-table-column
          type="index"
          width="80"
          label="序号">
        </el-table-column>
        <el-table-column
          prop="title"
          label="文章标题"
          width="350">
          <template slot-scope="scope">
            <span>{{ scope.row.title }}</span>
            <i class="el-icon-film" v-if="scope.row.videoURL" style="font-size: 20px; color: deeppink"  @click="video(scope.row.videoURL)"></i>
          </template>
        </el-table-column>
        <el-table-column
          prop="visits"
          label="阅读数"
          width="190">
        </el-table-column>
        <el-table-column
          prop="username"
          label="录入人"
          width="190">
        </el-table-column>
        <el-table-column
          prop="createTime"
          label="录入时间"
          width="250">
          <template slot-scope="scope">
            {{ scope.row.createTime | parseTimeByString }}
          </template>
        </el-table-column>
        <el-table-column
          prop="state"
          label="状态"
          width="80">
          <template slot-scope="scope">
            {{ mydata(scope.row.state) }}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="200">
          <template slot-scope="scope">
            <el-button @click="previewEditDialog(scope.row)" type="text" size="small">预览</el-button>
            <el-button @click="forbiddenEditDialog(scope.row)" type="text" size="small">
              {{ scope.row.state === 1 ? '禁用' : '启用' }}
            </el-button>
            <el-button @click="alterEditDialog(scope.row)" type="text" size="small"
                       :disabled="scope.row.state === 1 ? true : false">修改
            </el-button>
            <el-button @click="deleteEditDialog(scope.row.id)" type="text" size="small"
                       :disabled="scope.row.state === 1 ? true : false">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页功能 -->
      <div class="block">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="container.page"
          :page-sizes="[1, 2, 5, 10]"
          :page-size="container.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
    <!-- 新增试题弹出层 -->
    <el-dialog
      title="新增试题"
      :visible.sync="addDialogVisible"
      width="50%" @close='addDialogVisibleClose'>
      <el-form :model="addRuleForm" :rules="addRuleFormRules" ref="addRuleFormRef" label-width="100px">
        <el-form-item label="文章标题" prop='title'>
          <el-input v-model="addRuleForm.title"></el-input>
        </el-form-item>
        <el-form-item label='文章内容' prop='articleBody'>
          <quill-editor v-model="addRuleForm.articleBody">
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址" prop='videoURL'>
          <el-input v-model="addRuleForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="ensureDialogVisible('addRuleForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 预览弹出层 -->
    <el-dialog
      title="预览文章"
      :visible.sync="previewDialogVisible"
      width="60%"
      v-if="previewDialogVisible">
      <span>
        <questions-artcles :val="val"></questions-artcles>
      </span>
    </el-dialog>
    <!-- 修改弹出层 -->
    <el-dialog
      title="修改文章"
      :visible.sync="alterDialogVisible"
      width="50%" @close='addDialogVisibleClose'>
      <el-form :model="alterRuleForm" :rules="addRuleFormRules" ref="addRuleFormRef" label-width="100px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model="alterRuleForm.title"></el-input>
        </el-form-item>
        <el-form-item label='文章内容' prop="articleBody">
          <quill-editor v-model="alterRuleForm.articleBody">
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model="alterRuleForm.videoURL"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="alterDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="amendDialogVisible('alterRuleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { status } from '@/api/hmmm/constants'
import { changeState, remove, add, update, list } from '@/api/hmmm/articles'
import { parseTimeByString } from '@/filters'
import QuestionsArtcles from '../components/questions-artcles'

export default {
  name: 'Articles',
  components: {
    QuestionsArtcles
  },
  props: {},
  data () {
    return {
      // 视频的地址
      url: '',
      isDiv: false,
      parseTimeByString,
      status,
      // 关键字
      container: {
        page: 1,
        pagesize: 10
      },
      total: 0,
      // 获取到的全局数据
      containerQj: [],
      // 新增页面的显示与隐藏
      addDialogVisible: false,
      // 新增的数据
      addRuleForm: {
        title: '', // 文章标题
        articleBody: '', // 文章正文
        videoURL: '' // 视频地址
      },
      addRuleFormRules: {
        title: [
          { required: true, message: '请输入文章标题', trigger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章正文', trigger: 'blur' }
        ]
      },
      // 预览弹出层显示与隐藏
      previewDialogVisible: false,
      // 预览id
      val: '',
      // 修改弹出层显示与隐藏
      alterDialogVisible: false,
      alterRuleForm: {
        title: '', // 文章标题
        articleBody: '', // 文章正文
        videoURL: '', // 视频地址
        id: ''
      }
    }
  },
  computed: {
    mydata: function () {
      return function (val) {
        if (val === 0) {
          return this.status[1].label
        } else {
          return this.status[0].label
        }
      }
    }
  },
  watch: {},
  created () {
    // 发起请求获取数据
    this.getArticles()
  },
  mounted () {
  },
  methods: {
    // 监听pagination 改变事件
    handleSizeChange (newSize) {
      // console.log(newSize);
      this.container.pagesize = newSize
      this.getArticles()
    },
    // 监听 页码值 改变事件
    handleCurrentChange (newPage) {
      // console.log(newPage);
      this.container.page = newPage
      this.getArticles()
    },
    // 点击X 关闭
    close () {
      this.isDiv = false
    },
    // 点击视频按钮
    video (url) {
      // console.log(1)
      this.isDiv = true
      this.url = url
    },
    async getArticles () {
      const { data: res } = await list(this.container)
      this.containerQj = res.items
      this.total = res.counts
    },
    // 清除
    qingBtn () {
      this.container = {}
    },
    // 搜索
    async seekBtn () {
      this.getArticles()
    },

    // 新增模块
    newlyBtn () {
      this.addDialogVisible = true
    },
    // 监听对话框关闭事件
    addDialogVisibleClose () {
      this.$refs.addRuleFormRef.resetFields()
    },
    // 点击提交新增数据
     ensureDialogVisible() {
      this.$refs.addRuleFormRef.validate(value => {
        if (!value) return
        try {
          add({
            title: this.addRuleForm.title,
            articleBody: this.addRuleForm.articleBody,
            videoURL: this.addRuleForm.videoURL
          })
          this.addDialogVisible = false
          this.getArticles()
          this.$message({
            type: 'success',
            message: '添加成功!'
          })
        } catch (err) {
          this.$message({
            type: 'info',
            message: '添加失败'
          })
        }
      })
    },
    // 预览按钮
    previewEditDialog (val) {
      console.log(val)
      // console.log(123)
      // console.log(id)
      this.val = val
      this.previewDialogVisible = true
    },
    // 禁用 启用按钮
    async forbiddenEditDialog (val) {
      try {
        const stat = val.state === 1 ? '0' : '1'
        await changeState({ id: val.id, state: stat })
        this.getArticles()
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
      } catch (err) {
        this.$message({
          type: 'info',
          message: '修改失败'
        })
      }
      // 刷新页面
    },
    // 修改按钮
    alterEditDialog (val) {
      this.alterRuleForm.title = val.title
      this.alterRuleForm.articleBody = val.articleBody
      this.alterRuleForm.videoURL = val.videoURL
      this.alterRuleForm.id = val.id
      this.alterDialogVisible = true
    },
    // 点击修改文章
    amendDialogVisible () {
      this.$refs.addRuleFormRef.validate(async value => {
        if (!value) return
        try {
          await update({
            title: this.alterRuleForm.title,
            articleBody: this.alterRuleForm.articleBody,
            videoURL: this.alterRuleForm.videoURL,
            id: this.alterRuleForm.id
          })
          this.alterDialogVisible = false
          this.getArticles()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        } catch (err) {
          this.$message({
            type: 'info',
            message: '添加失败'
          })
        }
      })
    },
    // 删除按钮
    deleteEditDialog (id) {
      this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await remove({ id: id })
        this.getArticles()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='scss'>
.mask {
  /* 隐藏遮罩层 */
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, .4);
  z-index: 99999;
  //text-align: center;
}
.video {
  position: fixed;
  top: 20%;
  left: 25%;
}
.location {
  position: fixed;
  top: 10%;
  left: 48%;
}
.container {
  padding: 10px;

  .form-left {
    display: flex;
    justify-content: space-between;
    height: 35.99px;

    .form-div {
      display: flex;

      .form-button {
        margin-left: 10px;
      }
    }
  }

  .slot-icon {
    margin-top: 20px;
    background-color: #f4f4f5;
    color: #909399;
    height: 36px;
    font-size: 13px;
    line-height: 36px;
    padding-left: 25px;
    margin-bottom: 15px;
    border-radius: 5px;

    .tab-span {
      margin-left: 10px;
    }
  }

  .block {
    display: flex;
    flex-direction: row-reverse;
    margin-right: 20px;
    margin-top: 20px;

  }
}
</style>
